package com.ylkj.core;

import com.ylkj.common.util.RequestUtils;
import com.ylkj.core.vo.JsonResult;
import com.ylkj.core.vo.QueryResult;
import com.ylkj.core.vo.TipMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 控制层基类
 * @author 呼云飞
 *
 */
@SuppressWarnings({ "rawtypes", "unchecked","unused" })
public class BaesController {
	private Logger logger = LoggerFactory.getLogger(BaesController.class);
	@InitBinder  
    protected void initBinder(WebDataBinder binder) {  
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));  
    }

	/**
	 * 获取页面参数
	 * @param request
	 * @return
	 */
	public Map<String,Object> getQueryParams(HttpServletRequest request){
		Map<String, Object> pageMap = RequestUtils.getQueryParams(request);
		return pageMap;
	}
    /**
	 * 默认
	 * @return
	 */
	public <T> JsonResult buildJsonResult(){
        return new JsonResult(null,true, TipMessage.SUCCESS_MSG);
    }
	
	/**
	 * 默认
	 * @param obj
	 * @return
	 */
	public <T> JsonResult buildJsonResult(T obj){
        return new JsonResult(obj,true,TipMessage.SUCCESS_MSG);
    }
	
	/**
	 * 成功结果
	 * @param obj
	 * @param result
	 * @param msg
	 * @return
	 */
	public <T> JsonResult buildSuccessResult(T obj, boolean result, String msg){
        return new JsonResult(obj,result,msg);
    }
	
	/**
	 * 失败结果
	 * @param obj
	 * @param result
	 * @param msg
	 * @return
	 */
	public <T> JsonResult buildErrorResult(T obj, boolean result, String msg){
		return new JsonResult(obj,result,msg);
	}
	
	/**
	 * 封装结果集
	 * @return
	 */
	public <T> QueryResult buildQueryResult(List<T> rows, int total){
		return new QueryResult(rows,total);
	}

	/**
	 * 封装结果集
	 * @return
	 */
	public <T> QueryResult buildQueryResult(int page,int num,int total,List<T> data){
		return new QueryResult(page,num,total,data);
	}
	/**
	 * 封装结果集
	 * @return
	 */
	public <T> QueryResult buildQueryResult(boolean success, List<T> data, int total, int num, int page, int pageCount){
		return new QueryResult(success,data,total,num,page,pageCount);
	}

}
